class HomeController < ApplicationController

	def index
		list
		render('list')
	end

	def list
		@produces = Produce.order("produces.add_day DESC")
		@news = News.order("news.name DESC")
		@pro_1
		@pro_2
		@pro_3
		if session[:user]
			@cart = Cart.find(:first, :conditions => ["user_id = ?", session[:user].id])
			if @cart.cart_produces.size == 0
				@pro_1 = Produce.find(:all, :conditions => ["type_of_produce = ?", "coffee"]).sample(3)
				@pro_2 = Produce.find(:all,
					 :conditions => ["type_of_produce = ?", "snack"]).sample(1)
				@pro_3 = Produce.find(:all,
					 :conditions => ["type_of_produce = ?", "beverage"]).sample(1)
			elsif @cart.cart_produces.size == 1
				pro_temp = Produce.find(@cart.cart_produces[0].produce_id)
				if pro_temp.type_of_produce == "coffee"
					@pro_1 = Produce.find(:all,
					 :conditions => ["taste = ?", pro_temp.taste]).sample(3)
					@pro_2 = Produce.find(:all,
					 :conditions => ["type_of_produce = ?", "snack"]).sample(1)
					@pro_3 = Produce.find(:all,
					 :conditions => ["type_of_produce = ?", "beverage"]).sample(1)
				elsif pro_temp.type_of_produce == "snack"
					@pro_1 = Produce.find(:all,
					 :conditions => ["type_of_produce = ?", "coffee"]).sample(2)
					@pro_2 = Produce.find(:all,
					 :conditions => ["type_of_produce = ?", "snack"]).sample(2)
					@pro_3 = Produce.find(:all,
					 :conditions => ["type_of_produce = ?", "beverage"]).sample(1)
				else 
					@pro_1 = Produce.find(:all,
					 :conditions => ["type_of_produce = ?", "coffee"]).sample(2)
					@pro_2 = Produce.find(:all,
					 :conditions => ["type_of_produce = ?", "snack"]).sample(1)
					@pro_3 = Produce.find(:all,
					 :conditions => ["type_of_produce = ?", "beverage"]).sample(2)
				end
			elsif @cart.cart_produces.size == 2
				pro_temp = Produce.find(@cart.cart_produces[0].produce_id)
				pro_temp_2 = Produce.find(@cart.cart_produces[1].produce_id)
				if pro_temp.type_of_produce == "coffee"
					@pro_1 = Produce.find(:all,
					 :conditions => ["taste = ?", pro_temp.taste]).sample(2)
				else
					@pro_1 = Produce.find(:all,
					 :conditions => ["type_of_produce = ?", pro_temp.type_of_produce]).sample(2)
				end

				if pro_temp_2.type_of_produce == "coffee"

					if @pro_1.size == 2
						@pro_2 = Produce.find(:all,
						 :conditions => ["taste = ? AND name <> ? AND name <> ?", pro_temp_2.taste, 
						 	@pro_1[0].name, @pro_1[1].name]).sample(2)
					elsif @pro_1.size == 1
						@pro_2 = Produce.find(:all,
						 :conditions => ["taste = ? AND name <> ? ", pro_temp_2.taste, 
						 	@pro_1[0].name]).sample(2)
					else
						@pro_2 = Produce.find(:all,
						 :conditions => ["taste = ?", pro_temp_2.taste]).sample(2)
					end
							
				else
					if @pro_1.size == 2
						@pro_2 = Produce.find(:all,
						 :conditions => ["type_of_produce = ? AND name <> ? AND name <> ?", pro_temp_2.type_of_produce, 
						 	@pro_1[0].name, @pro_1[1].name]).sample(2)
					elsif @pro_1.size == 1
						@pro_2 = Produce.find(:all,
						 :conditions => ["type_of_produce = ? AND name <> ?", pro_temp_2.type_of_produce, 
						 	@pro_1[0].name]).sample(2)
					else 
						@pro_2 = Produce.find(:all,
						 :conditions => ["type_of_produce = ? ", pro_temp_2.type_of_produce]).sample(2)
					end
				end

				@pro_3 = Produce.find(:all, :conditions => ["type_of_produce <>? AND type_of_produce <> ? ", 
					pro_temp.type_of_produce, pro_temp_2.type_of_produce]).sample(1)

			else
				pro_temp = Produce.find(@cart.cart_produces[0].produce_id)
				pro_temp_2 = Produce.find(@cart.cart_produces[1].produce_id)
				pro_temp_3 = Produce.find(@cart.cart_produces[2].produce_id)
				if pro_temp.type_of_produce == "coffee"
					@pro_1 = Produce.find(:all,
					 :conditions => ["taste = ?", pro_temp.taste]).sample(2)
				else
					@pro_1 = Produce.find(:all,
					 :conditions => ["type_of_produce = ?", pro_temp.type_of_produce]).sample(2)
				end

				if pro_temp_2.type_of_produce == "coffee"

					if @pro_1.size == 2
						@pro_2 = Produce.find(:all,
						 :conditions => ["taste = ? AND name <> ? AND name <> ?", pro_temp_2.taste, 
						 	@pro_1[0].name, @pro_1[1].name]).sample(2)
					elsif @pro_1.size == 1
						@pro_2 = Produce.find(:all,
						 :conditions => ["taste = ? AND name <> ? ", pro_temp_2.taste, 
						 	@pro_1[0].name]).sample(2)
					else
						@pro_2 = Produce.find(:all,
						 :conditions => ["taste = ?", pro_temp_2.taste]).sample(2)
					end
							
				else
					if @pro_1.size == 2
						@pro_2 = Produce.find(:all,
						 :conditions => ["type_of_produce = ? AND name <> ? AND name <> ?", pro_temp_2.type_of_produce, 
						 	@pro_1[0].name, @pro_1[1].name]).sample(2)
					elsif @pro_1.size == 1
						@pro_2 = Produce.find(:all,
						 :conditions => ["type_of_produce = ? AND name <> ?", pro_temp_2.type_of_produce, 
						 	@pro_1[0].name]).sample(2)
					else 
						@pro_2 = Produce.find(:all,
						 :conditions => ["type_of_produce = ? ", pro_temp_2.type_of_produce]).sample(2)
					end
				end

				if pro_temp_3.type_of_produce == "coffee"
					if @pro_1.size == 2 
						if (@pro_2.size == 1)
							@pro_3 = Produce.find(:all, :conditions => ["taste = ? AND name <> ? AND name <> ? AND name <> ?", pro_temp_3.taste, 
							 	@pro_1[0].name, @pro_1[1].name, @pro_2[0].name]).sample(1)
						elsif (@pro_2.size == 2)
							@pro_3 = Produce.find(:all, :conditions => ["taste = ? AND name <> ? AND name <> ? AND name <> ? AND name <> ?", pro_temp_3.taste, 
							 	@pro_1[0].name, @pro_1[1].name, @pro_2[0].name, @pro_2[1].name]).sample(1)
						else 
							@pro_3 = Produce.find(:all,
							 :conditions => ["taste = ? AND name <> ? AND name <> ?", pro_temp_3.taste, 
							 	@pro_1[0].name, @pro_1[1].name]).sample(1)
						end
					elsif @pro_1.size == 1
						if (@pro_2.size == 1)
							@pro_3 = Produce.find(:all, :conditions => ["taste = ? AND name <> ? AND name <> ?", pro_temp_3.taste, 
							 	@pro_1[0].name,  @pro_2[0].name]).sample(1)
						elsif (@pro_2.size == 2)
							@pro_3 = Produce.find(:all, :conditions => ["taste = ?  AND name <> ? AND name <> ? AND name <> ?", pro_temp_3.taste, 
							 	@pro_1[0].name,  @pro_2[0].name, @pro_2[1].name]).sample(1)
						else 
							@pro_3 = Produce.find(:all,
							 :conditions => ["taste = ? AND name <> ?", pro_temp_3.taste, 
							 	@pro_1[0].name]).sample(1)
						end

					else 

						if (@pro_2.size == 1)
							@pro_3 = Produce.find(:all, :conditions => ["taste = ? AND name <> ?", pro_temp_3.taste, 
							 	@pro_2[0].name]).sample(1)
						elsif (@pro_2.size == 2)
							@pro_3 = Produce.find(:all, :conditions => ["taste = ?  AND name <> ? AND name <> ?", pro_temp_3.taste, 
							   @pro_2[0].name, @pro_2[1].name]).sample(1)
						else 
							@pro_3 = Produce.find(:all,
						 :conditions => ["taste = ? ", pro_temp_3.taste]).sample(1)
						end
					end
						
				else
					@pro_3 = Produce.find(:all,
					 :conditions => ["type_of_produce = ?, AND name <> ? AND name <> ? AND name <> ? AND name <> ?", 
					 	pro_temp_3.type_of_produce, @pro_1[0].name, @pro_1[1].name, @pro_2[0].name, @pro_2[1].name]).sample(1)
				end
			end
		end
	end

	def show
		@produce = Produce.find(params[:id])
	end

end
